import PIL
from PIL import Image

import SimpleITK as sitk
import os
import nrrd
import cv2
import nibabel as nib
import numpy as np

data_path = r'D:\install\pytorch-1.3\project\miccai-code\data\P26\1'
data_path = r'D:\install\pytorch-1.3\project\miccai-code\data\P26\1'
save_path = r'D:\install\pytorch-1.3\project\miccai-code\nii'

data, options = nrrd.read(data_path + r'\Segmentation.seg.nrrd')  # 读入 nrrd 文件
data1, options1 = nrrd.read(data_path + r'\1.nrrd')  # 读入 nrrd 文件

# 可以打印查看data 和 nrrd_options 里面的内容：
print(data.shape)
print(data1[1])
for data1 in enumerate(data1):
    image = sitk.ReadImage(data1[1])
    print(image)


# print(options)

# img = nib.Nifti1Image(data, np.eye(4))  # 将 nrrd 文件转换为 .nii 文件
# nib.save(img, save_path)  # 保存 nii 文件

def nrrd_to_png(nrrd_filename, save_path):
    nrrd_filename = nrrd_filename
    nrrd_data, nrrd_options = nrrd.read(nrrd_filename)
    h, w, slides_num = nrrd_data.shape
    for i in range(slides_num):
        img = nrrd_data[:, :, slides_num - i - 1] * 255
        # img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        cv2.imwrite(save_path + '/' + '_' + str(i + 1) + '.png', img)

def rotation(path):
    image = cv2.imread(path)
    image = cv2.transpose(image)
    # image = cv2.flip(image,1)
    cv2.imwrite(path, image)
